﻿// <copyright file="InformTimeError.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author>

namespace StcEthernet.Management
{
  using System;
  using EnOcean;

  /// <summary>
  /// This telegram is sent from the wireless receiver to the application, if the monitor timer is exceeded.
  /// </summary>
  [Serializable]
  public class InformTimeError : ManagementTelegram
  {
    /// <summary>
    /// Variable for the Id property.
    /// </summary>
    private long id;

    /// <summary>
    /// Initializes a new instance of the InformTimeError class.
    /// </summary>
    public InformTimeError()
    {
    }
      
    /// <summary>
    /// Gets or sets the org byte.
    /// </summary>
    public Org Org
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets a value indicating whether the mointor timer is on.
    /// </summary>
    public bool IsMonitorTimerOn
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the monitor interval.
    /// </summary>
    public byte MonitorInterval
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets a value indicating wheter the sensor fails from time to time. No: “00“; Yes: “01-FF“
    /// </summary>
    public byte SensorFailureFromTimeToTime
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets a value indicating whether the sensor has failed.
    /// </summary>
    public bool HasFailed
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the channel to use for this transmitter.
    /// </summary>
    public byte Channel
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the 32 bit transmitter ID. The ID must be in the range 0 to 0xffffffff inclusive.
    /// </summary>
    public long Id
    {
      get
      {
        return this.id;
      }

      set
      {
        if (value >= 0x0 && value <= 0xffffffff)
        {
          this.id = value;
        }
        else
        {
          throw new ArgumentOutOfRangeException("Id", "must be a 32 bit value in the range 0x0 to 0xffffffff inclusive");
        }
      }
    }

    /// <summary>
    /// Creates the byte format of the message.
    /// </summary>
    /// <param name="message">A byte array of length 14 to hold the message contents.</param>
    protected internal override void CreateMessage(byte[] message)
    {
      byte[] i;

      i = BitConverter.GetBytes(this.Id);

      if (message == null)
      {
        throw new ArgumentNullException("message");
      }

      if (message.Length < 10)
      {
        throw new ArgumentOutOfRangeException("message", "array length must be at least 14 to hold the message content.");
      }

      message[0] = 0xA5; // Sync
      message[1] = 0x5B; // Sync
      message[2] = 0x98;
      message[3] = (byte)this.Org;
      message[4] = this.IsMonitorTimerOn ? (byte)0xF0 : (byte)0xFF;
      message[5] = this.MonitorInterval;
      message[6] = this.SensorFailureFromTimeToTime;
      message[7] = this.HasFailed ? (byte)0x01 : (byte)0x00;
      message[8] = i[3];
      message[9] = i[2];
      message[10] = i[1];
      message[11] = i[0];
      message[12] = 0x00;
      message[13] = this.Channel;
    }
  }
}
